<table>
    <tr>
        <td colspan="7">
            <?php echo $this->element('excel/title'); ?>
            <table>
                                <?php if (!empty($criteria['Product']['part_no'])) : ?>
                <tr>
                    <td width="90px" class="label">Stock Code</td>
                    <td ><?php echo $criteria['Product']['part_no']; ?></td>
                </tr>
                <?php endif; ?>
                <?php if (!empty($criteria['Dispatch']['customer_id'])) : ?>
                <tr>
                    <td width="90px" class="label">Customer</td>
                    <td ><?php echo $customer_name; ?></td>
                </tr>
                <?php endif; ?>
                <?php if (!empty($criteria['Dispatch']['site_id'])) : ?>
                <tr>
                    <td width="90px" class="label">Site</td>
                    <td ><?php echo $site_name; ?></td>
                </tr>
                <?php endif; ?>
                <?php if (!empty($criteria['Product']['category_id'])) : ?>
                <tr>
                    <td width="90px" class="label">Category</td>
                    <td ><?php echo $category_name; ?></td>
                </tr>
                <?php endif; ?>
                <?php if (!empty($criteria['Product']['supplier_id'])) : ?>
                <tr>
                    <td width="90px" class="label">Vendor</td>
                    <td ><?php echo $vendor_name; ?></td>
                </tr>
                <?php endif; ?>
                <?php if (!empty($criteria['Product']['brand_id'])) : ?>
                <tr>
                    <td width="90px" class="label">Brand</td>
                    <td ><?php echo $brand_name; ?></td>
                </tr>
                <?php endif; ?>
                <?php if (!empty($criteria['Dispatch']['dispatch_date_start'])) : ?>
                <tr>
                    <td width="90px" class="label">Dispatch date</td>
                    <td >From <?php echo $criteria['Dispatch']['dispatch_date_start']; ?> to <?php echo $criteria['Dispatch']['dispatch_date_end']; ?></td>
                </tr>
                <?php endif; ?>
                <?php if (!empty($criteria['Option']['order1'])) : ?>
                <tr>
                    <td width="90px" class="label">Order by</td>
                    <td >
                            <?php echo !empty($criteria['Option']['order1'])? $orderOptions[$criteria['Option']['order1']]: ''; ?>
                            <?php echo !empty($criteria['Option']['order2'])? $orderOptions[$criteria['Option']['order2']]: ''; ?>
                            <?php echo !empty($criteria['Option']['seq']) && $criteria['Option']['seq'] == 'desc'? ' Descendent': ' Ascendent'; ?>
                    </td>
                </tr>
                <?php endif; ?>
                <?php if (!empty($criteria['Option']['limit'])) : ?>
                <tr>
                    <td width="90px" class="label">Show</td>
                    <td><?php echo $criteria['Option']['limit']; ?> entries</td>
                </tr>
                <?php endif; ?>
            </table>
        </td>
        <td colspan="4">
            <table>
                <tr>
                    <td colspan="4" class="docNote">VMI REPORT: ITS Dispatch</td>
                </tr>
                <tr>
                    <td class="label">Report date</td>
                    <td><?php echo $report_date; ?></td>
                    <td class="label">Report by</td>
                    <td><?php echo $user; ?></td>
                </tr>
            </table>
        </td>
    </tr>
</table>
<!--div class="header">
</div-->

<div class="main">
    <table id="dataTable" class="dataTable" cellspacing="0" cellpadding="0">
        <thead>
            <tr>
                <th class="no">No.</th>
                <th>Part no.<?php if ($display_dispatch_no == true) echo '/ <br /> Dispatch no.'; ?></th>
                <th>Stock code <?php if ($display_dispatch_no == true) echo '/ <br /> Dispatch Date'; ?></th>
                <th>Customer no.</th>
                <th>Customer Name  <?php if ($display_dispatch_no == true) echo '/ <br /> Product Description'; ?></th>
                <th>Site no.</th>
                <th>Site Name</th>
                <th width="40px">Freq.</th>
                <th width="45px">Price</th>
                <th width="40px">Qty.*</th>
                <th width="50px">Amount</th>
            </tr>
        </thead>
        <tbody>
            <?php $i = 0;
            $r = 0;
            $total_freq = 0;
            $total_qty = 0;
            $total_amount = 0;
            foreach($this->data as $i => $data): ?>
            <tr>
                <td class="center"><?php echo ($i + 1); ?></td>
                <td class="<?php echo $display_dispatch_no == true? 'bold': ''; ?>">
                        <?php echo $data['Product']['part_no']; ?>
                </td>
                <td class="<?php echo $display_dispatch_no == true? 'bold': ''; ?>">
                        <?php echo $data['CustProductSite']['stock_code']; ?>
                </td>
                <td class="<?php echo $display_dispatch_no == true? 'bold': ''; ?>"><?php echo $data['Customer']['cust_no']; ?></td>
                <td class="<?php echo $display_dispatch_no == true? 'bold': ''; ?>"><?php echo $data['Customer']['name']; ?></td>
                <td class="<?php echo $display_dispatch_no == true? 'bold': ''; ?>"><?php echo $data['Site']['site_no']; ?></td>
                <td class="<?php echo $display_dispatch_no == true? 'bold': ''; ?>"><?php echo $data['Site']['name']; ?></td>
                <td class="text-right<?php echo $display_dispatch_no == true? 'bold': ''; ?>"><?php echo $data[0]['frequency']; ?></td>
                <td class="text-right<?php echo $display_dispatch_no == true? 'bold': ''; ?>"><?php echo number_format($data[0]['price'], 2); ?></td>
                <td class="text-right<?php echo $display_dispatch_no == true? 'bold': ''; ?>"><?php echo $data[0]['stock_in_qty']; ?></td>
                <td class="text-right<?php echo $display_dispatch_no == true? 'bold': ''; ?>"><?php echo number_format($data[0]['price'] * $data[0]['stock_in_qty'], 2); ?></td>
                    <?php
                    $total_freq += $data[0]['frequency'];
                    $total_qty += $data[0]['stock_in_qty'];
                    $total_amount += $data[0]['price'] * $data[0]['stock_in_qty'];
                    ?>
            </tr>
                <?php if ($display_dispatch_no == true) :
                    $stos = $this->requestAction('its_reports/get_dispatch_no/' . $data['Product']['id'] . '/' . $data['CustProductSite']['id']);
                    // ตัวแปรสำหรับลำดับเลข stock out no
                    $k = 1;
                    foreach($stos as $sto) :
                        ?>
            <tr>
                <td class="rowTd">&nbsp;</td>
                            <?php if ($display_dispatch_no == true) : ?>
                <td>
                    <span><?php echo $k; ?>.</span>&nbsp;
                    <span><?php echo $sto['Dispatch']['dispatch_no']; ?></span>
                </td>
                <td><?php echo $sto[0]['dispatch_date']; ?></td>
                <td>&nbsp;</td>
                <td><?php echo $k == 1? $data['Product']['text']: '&nbsp;'; ?></td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td class="text-right"><?php echo $sto['CustStockIn']['stock_in_qty']; ?></td>
                <td class="text-right"><?php echo number_format($sto['CustStockIn']['stock_in_qty'] * $data[0]['price'], 2); ?></td>
                            <?php endif; ?>
            </tr>
                        <?php ++$k;
                    endforeach; ?>
                <?php endif; ?>
            <?php
            endforeach; ?>
            <tr class="trFooter">
                <td colspan="7" class="text-right">Grand Total</td>
                <td class="bold"><?php echo number_format($total_freq); ?></td>
                <td>-</td>
                <td class="bold"><?php echo number_format($total_qty); ?></td>
                <td class="bold"><?php echo number_format($total_amount, 2); ?></td>
            </tr>
        </tbody>
    </table>
</div>

<div class="footer"></div>

<div id="divLastFooter"></div>